//
//  SLMP4Parser+Moov.m
//  SLVideoParser
//
//  Created by S_LINE on 2020/6/19.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLMP4Parser+Moov.h"

#import "SLParserUtil.h"

@implementation SLMP4Parser (Moov)

- (NSDictionary *)mvhdParser:(NSData *)data
{
    if (data.length < 1)
    {
        return nil;
    }
    NSString *versionKey = @"version";
    NSString *flagsKey = @"flags";
    NSString *timeScaleKey = @"timescale";
    NSString *durationKey = @"duration";
    NSString *mediaDurationKey = @"mediaDuration";
    NSString *rateKey = @"rate";
    NSString *volumeKey = @"volume";
    NSString *nextTrackIdKey = @"nextTrackId";
    NSMutableDictionary *dict = [NSMutableDictionary new];
    
    NSData *versionData = [data subdataWithRange:NSMakeRange(0, 1)];
    uint64_t version = [SLParserUtil hexToDecimalWithData:versionData];
    [dict setObject:[NSNumber numberWithUnsignedLongLong:version] forKey:versionKey];
    uint64_t offset = 1;
    if (version == 0)
    {
        uint64_t flagsSize = 3;
        NSData *flagsData = [data subdataWithRange:NSMakeRange(offset, flagsSize)];
        uint64_t flags = [SLParserUtil hexToDecimalWithData:flagsData];
        offset += flagsSize;
        // creation time 创建时间（相对于UTC时间1904-01-01零点的秒数）
        uint64_t creationTimeSize = 4;
        offset += creationTimeSize;
        // modification time 修改时间
        uint64_t modificationTimeSize = 4;
        offset += modificationTimeSize;
        // time scale 文件媒体在1秒时间内的刻度值，可以理解为1秒长度的时间单元数
        uint64_t timeScaleSize = 4;
        NSData *timeScaleData = [data subdataWithRange:NSMakeRange(offset, timeScaleSize)];
        uint64_t timeScale = [SLParserUtil hexToDecimalWithData:timeScaleData];
        offset += timeScaleSize;
        // duration 该track的时间长度，用duration和time scale值可以计算track时长，比如audio track的time scale = 8000, duration = 560128，时长为70.016，video track的time scale = 600, duration = 42000，时长为70
        uint64_t durationSize = 4;
        NSData *durationData = [data subdataWithRange:NSMakeRange(offset, durationSize)];
        uint64_t duration = [SLParserUtil hexToDecimalWithData:durationData];
        // 整个视频的长度
        double mediaDuration = ((double)duration) / ((double)timeScale);
        offset += durationSize;
        // rate 推荐播放速率，高16位和低16位分别为小数点整数部分和小数部分，即[16.16] 格式，该值为1.0（0x00010000）表示正常前向播放
        uint64_t integerRateSize = 2;
        NSData *integerRateData = [data subdataWithRange:NSMakeRange(offset, integerRateSize)];
        uint64_t integeRate = [SLParserUtil hexToDecimalWithData:integerRateData];
        offset += integerRateSize;
        uint64_t floatRateSize = 2;
        NSData *floatRateData = [data subdataWithRange:NSMakeRange(offset, floatRateSize)];
        uint64_t floatRate = [SLParserUtil hexToDecimalWithData:floatRateData];
        offset += floatRateSize;
        // volume 与rate类似，[8.8] 格式，1.0（0x0100）表示最大音量
        uint64_t integerVolumeSize = 1;
        NSData *integerVolumeData = [data subdataWithRange:NSMakeRange(offset, integerVolumeSize)];
        uint64_t integerVolume = [SLParserUtil hexToDecimalWithData:integerVolumeData];
        offset += integerVolumeSize;
        uint64_t floatVolumeSize = 1;
        NSData *floatVolumeData = [data subdataWithRange:NSMakeRange(offset, floatVolumeSize)];
        uint64_t floatVolume = [SLParserUtil hexToDecimalWithData:floatVolumeData];
        offset += floatVolumeSize;
        // reserved 保留位
        uint64_t reservedSize = 10;
        offset += reservedSize;
        // matrix 视频变换矩阵
        uint64_t matrixSize = 36;
        offset += matrixSize;
        // pre-defined
        uint64_t preDefinedSize = 24;
        offset += preDefinedSize;
        // next track id
        uint64_t nextTrackIdSize = 4;
        NSData *nexTrackIdData = [data subdataWithRange:NSMakeRange(offset, nextTrackIdSize)];
        uint64_t nexTrackId = [SLParserUtil hexToDecimalWithData:nexTrackIdData];
        offset += nextTrackIdSize;
        
        [dict setObject:[NSNumber numberWithUnsignedLongLong:flags] forKey:flagsKey];
        [dict setObject:[NSNumber numberWithUnsignedLongLong:timeScale] forKey:timeScaleKey];
        [dict setObject:[NSNumber numberWithUnsignedLongLong:duration] forKey:durationKey];
        [dict setObject:[NSNumber numberWithUnsignedLongLong:mediaDuration] forKey:mediaDurationKey];
        NSString *rateString = [NSString stringWithFormat:@"%llu.%llu", integeRate, floatRate];
        NSDecimalNumber *rateNumber = [NSDecimalNumber decimalNumberWithString:rateString];
        if ([rateNumber isKindOfClass:[NSNumber class]])
        {
            [dict setObject:rateNumber forKey:rateKey];
        }
        NSString *volumeString = [NSString stringWithFormat:@"%llu.%llu", integerVolume, floatVolume];
        NSDecimalNumber *volumeNumber = [NSDecimalNumber decimalNumberWithString:volumeString];
        if ([volumeNumber isKindOfClass:[NSNumber class]])
        {
            [dict setObject:volumeNumber forKey:volumeKey];
        }
        [dict setObject:[NSNumber numberWithUnsignedLongLong:nexTrackId] forKey:nextTrackIdKey];
    }
    
    return [dict copy];
}

@end
